﻿Imports System
Imports System.Collections.Generic
Imports System.Workflow.ComponentModel

Namespace System.Workflow.Runtime.Tracking
    <Serializable> _
    Public NotInheritable Class ActivityTrackingLocation
        ' Methods
        Public Sub New()
            Me._conditions = New TrackingConditionCollection
            Me._events = New List([Of] ActivityExecutionStatus)
        End Sub

        Public Sub New(ByVal activityTypeName As String)
            Me._conditions = New TrackingConditionCollection
            Me._events = New List([Of] ActivityExecutionStatus)
            If (activityTypeName Is Nothing) Then
                Throw New ArgumentNullException("activityTypeName")
            End If
            Me._activityName = activityTypeName
        End Sub

        Public Sub New(ByVal activityType As Type)
            Me._conditions = New TrackingConditionCollection
            Me._events = New List([Of] ActivityExecutionStatus)
            If (activityType Is Nothing) Then
                Throw New ArgumentNullException("activityType")
            End If
            Me._activityType = activityType
        End Sub

        Public Sub New(ByVal activityTypeName As String, ByVal executionStatusEvents As IEnumerable([Of] ActivityExecutionStatus))
            Me._conditions = New TrackingConditionCollection
            Me._events = New List([Of] ActivityExecutionStatus)
            If (activityTypeName Is Nothing) Then
                Throw New ArgumentNullException("activityTypeName")
            End If
            If (executionStatusEvents Is Nothing) Then
                Throw New ArgumentNullException("executionStatusEvents")
            End If
            Me._activityName = activityTypeName
            Me._events.AddRange(executionStatusEvents)
        End Sub

        Public Sub New(ByVal activityType As Type, ByVal executionStatusEvents As IEnumerable([Of] ActivityExecutionStatus))
            Me._conditions = New TrackingConditionCollection
            Me._events = New List([Of] ActivityExecutionStatus)
            If (activityType Is Nothing) Then
                Throw New ArgumentNullException("activityType")
            End If
            If (executionStatusEvents Is Nothing) Then
                Throw New ArgumentNullException("executionStatusEvents")
            End If
            Me._activityType = activityType
            Me._events.AddRange(executionStatusEvents)
        End Sub

        Public Sub New(ByVal activityTypeName As String, ByVal matchDerivedTypes As Boolean, ByVal executionStatusEvents As IEnumerable([Of] ActivityExecutionStatus))
            Me._conditions = New TrackingConditionCollection
            Me._events = New List([Of] ActivityExecutionStatus)
            If (activityTypeName Is Nothing) Then
                Throw New ArgumentNullException("activityTypeName")
            End If
            If (executionStatusEvents Is Nothing) Then
                Throw New ArgumentNullException("executionStatusEvents")
            End If
            Me._activityName = activityTypeName
            Me._trackDerived = matchDerivedTypes
            Me._events.AddRange(executionStatusEvents)
        End Sub

        Public Sub New(ByVal activityType As Type, ByVal matchDerivedTypes As Boolean, ByVal executionStatusEvents As IEnumerable([Of] ActivityExecutionStatus))
            Me._conditions = New TrackingConditionCollection
            Me._events = New List([Of] ActivityExecutionStatus)
            If (activityType Is Nothing) Then
                Throw New ArgumentNullException("activityType")
            End If
            If (executionStatusEvents Is Nothing) Then
                Throw New ArgumentNullException("executionStatusEvents")
            End If
            Me._activityType = activityType
            Me._trackDerived = matchDerivedTypes
            Me._events.AddRange(executionStatusEvents)
        End Sub

        Private Function ConditionsAreMatch(ByVal obj As Object) As Boolean
            Dim condition As TrackingCondition
            For Each condition In Me._conditions
                If Not condition.Match(obj) Then
                    Return False
                End If
            Next
            Return True
        End Function

        Friend Function Match(ByVal activity As Activity, ByVal typeMatchOnly As Boolean) As Boolean
            If (activity Is Nothing) Then
                Throw New ArgumentNullException("activity")
            End If
            If Not Me.TypeIsMatch(activity) Then
                Return False
            End If
            Return (typeMatchOnly OrElse Me.ConditionsAreMatch(activity))
        End Function

        Private Function TypeIsMatch(ByVal activity As Activity) As Boolean
            If (Not Me._activityType Is Nothing) Then
                Return TypeMatch.IsMatch(activity, Me._activityType, Me._trackDerived)
            End If
            Return TypeMatch.IsMatch(activity, Me._activityName, Me._trackDerived)
        End Function


        ' Properties
        Public Property ActivityType As Type
            Get
                Return Me._activityType
            End Get
            Set(ByVal value As Type)
                Me._activityType = value
            End Set
        End Property

        Public Property ActivityTypeName As String
            Get
                Return Me._activityName
            End Get
            Set(ByVal value As String)
                Me._activityName = value
            End Set
        End Property

        Public ReadOnly Property Conditions As TrackingConditionCollection
            Get
                Return Me._conditions
            End Get
        End Property

        Public ReadOnly Property ExecutionStatusEvents As IList([Of] ActivityExecutionStatus)
            Get
                Return Me._events
            End Get
        End Property

        Public Property MatchDerivedTypes As Boolean
            Get
                Return Me._trackDerived
            End Get
            Set(ByVal value As Boolean)
                Me._trackDerived = value
            End Set
        End Property


        ' Fields
        Private _activityName As String
        Private _activityType As Type
        Private _conditions As TrackingConditionCollection
        Private _events As List([Of] ActivityExecutionStatus)
        Private _trackDerived As Boolean
    End Class
End Namespace

